/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import control.AuctionControl;
import control.ListenerControl;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import model.*;
import utility.CardHandler;

/**
 *
 * @author markh_000
 */
public class MainFrame1 extends javax.swing.JFrame implements ActionListener {

    private CardLayout cl;
    private CardHandler ch;
    private AuctionControl ac;
    private ListenerControl lc;
    private User buyer;
    private AuctionSiden as;
    private CreateProduct cp;
    private HovdePanel hp;
    public static final String HOVEDPANEL = "hp";
    public static final String AUCTIONSIDE = "as";
    public static final String CREATEPRODUCT = "cp";

    /**
     * Creates new form main
     *
     * @param ac
     * @param buyer
     * @param lc
     */
    public MainFrame1(AuctionControl ac, User buyer, ListenerControl lc) {
        this.ac = ac;
        this.buyer = buyer;
        this.lc = lc;
        lc.addListener(this);
        initComponents();
        setTitle(buyer.getName());
        setSize(new Dimension(750, 600));
        ch = new CardHandler(cl, jPanel1, this);
        addPages();

    }

    public void addPages() {
        hp = new HovdePanel(ac, buyer, this);
        ch.addPage(hp, HOVEDPANEL);

        as = new AuctionSiden(buyer, this);
        ch.addPage(as, AUCTIONSIDE);
        ch.show(HOVEDPANEL);

        cp = new CreateProduct(ac, buyer, this);
        ch.addPage(cp, CREATEPRODUCT);
    }

    public CardHandler getCardHandler() {
        return ch;
    }

    public AuctionSiden getAuctionSiden() {
        return as;
    }

    public void createProduct(Auction auction) {
        ac.addAuction(auction);
    }

    public void notifyListeners(String msg) {
        lc.notifyListeners(msg);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setLayout(new java.awt.CardLayout());

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainFrame1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainFrame1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainFrame1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainFrame1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {

            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables

    @Override
    public void actionPerformed(ActionEvent e) {
        switch (e.getActionCommand()) {
            case "New Auction":
                hp.update();
                break;
            case "New Bid":
                as.setLatestBid();
                hp.setLatestBid();
                break;
            default:
                System.out.println("Unknown Command... Updating everything...");
                hp.update();
                as.setLatestBid();
                hp.setLatestBid();
                break;
        }

    }
}
